package com.example.vv.model.register.base.presenter;

import android.util.Log;
import android.widget.Toast;

import com.example.vv.fragmentWork.entity.RegisterEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.fragmentWork.mvp.ViewISNullException;
import com.example.vv.model.register.base.contract.RegisterContract;
import com.example.vv.model.register.base.model.RegisterModel;
import com.example.vv.req.REQ_REGISTER;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class RegisterPresenter extends MvpPresenter<RegisterContract.View, RegisterContract.Model> implements RegisterContract.Presenter {
    @Override
    public void attach(RegisterContract.View view) {
        super.attach(view);
        model = new RegisterModel();
    }

    @Override
    public void register() {
        try {
            REQ_REGISTER re = new REQ_REGISTER(view().getUserName(),view().getPassWord(),view().getRePassWord());
            model.req_register(re)
                    .map(new Function<BaseResponse, RegisterEntity>() {
                        @Override
                        public RegisterEntity apply(BaseResponse baseResponse) throws Exception {
                            String data = baseResponse.getData();
                            view().getMsg(baseResponse.getErrorMsg());
                            RegisterEntity registerEntity = new Gson().fromJson(data, RegisterEntity.class);
                            return registerEntity;
                        }
                    }).subscribe(new Consumer<RegisterEntity>() {
                @Override
                public void accept(RegisterEntity registerEntity)  {
                    try {
                        view().setRegisterEntity(registerEntity);
                    } catch (ViewISNullException e) {
                        e.printStackTrace();
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {

                }
            });
        } catch (ViewISNullException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isFormat() {
        /*try {
            boolean b = view().getUserName() != null && view().getPassWord() != null && view().getRePassWord() != null && view().getPassWord().length() >= 6 && view().getPassWord().length() <= 16 && view().getRePassWord().length() >= 6 && view().getRePassWord().length() <= 16;
            return b;
        } catch (ViewISNullException e) {
            e.printStackTrace();
            return false;
        }*/
        return true;
    }
}
